package com.example.administrator.yuejian23.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.yuejian23.Bean.RegisterBean;
import com.example.administrator.yuejian23.Const;
import com.example.administrator.yuejian23.R;
import com.example.administrator.yuejian23.Retrofit.RetrofitService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegActivity extends BaseActivity {
    private EditText telView;
    private EditText passwordView;
    private EditText password2View;
    private EditText photo;
    private EditText UserName;
    private Button regButton;
    private Button cancelButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_register);
        initView();
        setListener();


    }
    private void setListener(){
        regButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //提交注册信息
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Const.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                RetrofitService retrofitService =
                        retrofit.create(RetrofitService.class);
                final String tel = telView.getText().toString();
                String password = passwordView.getText().toString();
                String password2 = password2View.getText().toString();
               /* String username =UserName.getText().toString();*/




                if(!password.equals("") && password.equals(password2)){
                    Call<RegisterBean> regCall = retrofitService.userRegister(tel,password);
                    regCall.enqueue(new Callback<RegisterBean>() {
                        @Override
                        public void onResponse(Call<RegisterBean> call, Response<RegisterBean> response) {
                            RegisterBean r = response.body();
                            if (r.getId().equals("0")){
                                Toast.makeText(RegActivity.this, "注册失败", Toast.LENGTH_SHORT).show();

                            }else {

                                Intent data = new Intent();
                                data.putExtra("tel", tel);

                                setResult(RESULT_OK,data);
                                Toast.makeText(RegActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        }

                        @Override
                        public void onFailure(Call<RegisterBean> call, Throwable t) {

                        }
                    });
                }else {
                    Toast.makeText(RegActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                }


            }
        });
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(RESULT_CANCELED);
                finish();

            }
        });

    }

    private void initView(){
        telView = (EditText) findViewById(R.id.editText_tel);
        passwordView = (EditText) findViewById(R.id.editText_password);
        password2View = (EditText) findViewById(R.id.editText_password2);
        regButton = (Button) findViewById(R.id.button_register);
        cancelButton = (Button) findViewById(R.id.button_cancel);
    }

}
